//
//  SXSearchView.swift
//  weibo
//
//  Created by 孙新 on 16/4/17.
//  Copyright © 2016年 孙新. All rights reserved.
//

import UIKit

class SXSearchView: UIView {

    @IBOutlet weak var cancelButton: UIButton!
    @IBOutlet weak var textFiled: UITextField!
    @IBOutlet weak var textFiledRightCons: NSLayoutConstraint!
    override func awakeFromNib() {
        //设置textFiled的大小
        textFiled.frame.size = CGSize(width: self.frame.width, height: self.frame.height)
        //设置textFiled的左边图片
        textFiled.leftView = leftView
        //设置左边图片的显示样式
        textFiled.leftViewMode = .Always
    }
    
        //搜索框开始编辑
    @IBAction func editStart(sender: UITextField) {
        //当输入框称为响应者就让距离右边的约束变删除按钮的宽度 让取消按钮显示
        self.textFiledRightCons.constant = self.cancelButton.frame.size.width
        UIView.animateWithDuration(0.25) { () -> Void in
            //重新布局
            self.layoutIfNeeded()
        }
        print("可以编辑")
    }
      //取消按钮
    @IBAction func cancelClickButton(sender: UIButton) {
        //点击取消按钮让约束变为0
        self.textFiledRightCons.constant = 0
        //动画
        UIView.animateWithDuration(0.25) { () -> Void in
            //重新布局
            self.layoutIfNeeded()
        }
        //取消第一响应
        self.textFiled.resignFirstResponder()
        print("取消按钮被遮盖了")
        
    }
    //写个类方法让Discover调用加载Xib
    class func loadSeachViewXib() -> SXSearchView {
        return NSBundle.mainBundle().loadNibNamed("SXSeachView", owner: nil, options: nil).last! as! SXSearchView
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    

   
    
  
    
    
    
    
    //懒加载图片
    private lazy var leftView: UIImageView = {
        let leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        //设置内容模式
        leftView.contentMode = .Center
        return leftView
    }()

}
